package com.baidu.thirdlib.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.thirdlib.BaseActivity;
import com.baidu.thirdlib.R;
import com.kvin.toolkit.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class SMSActivity extends BaseActivity {

    @BindView(R.id.launch_btn)
    Button lunchBtn;
    @BindView(R.id.no_et)
    EditText noEt;
    private RegisterPage rp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        ButterKnife.bind(this);
        init();
    }


    /**
     * without surface
     * step-1> SMSSDK.initSDK(this, "f800d28aaddb", "7c9f183c441af841aa0491c29ecfcdf4", true);//initialize
     * param1:context param2:app key param3:app secret
     * <p/>
     * step-2>  SMSSDK.registerEventHandler(mEventHandler);//register handler
     * param:callback
     * <p/>
     * step-3> SMSSDK.getVerificationCode(String.valueOf(86), noEt.getText().toString(), mOnSendMessageHandler);//get verification code
     * param1:countryCode param2:phone number param3:
     * <p/>
     * <p/>
     * other:
     * 》SMSSDK.getSupportedCountries();//return supported countries ;{@link #mEventHandler}
     * <p/>
     * 》you can define your msg sample ：短信验证码243156 ，请勿泄露(神舟科技)；
     */
    public void init() {
        SMSSDK.initSDK(this, "f800d28aaddb", "7c9f183c441af841aa0491c29ecfcdf4", true);
        lunchBtn.setOnClickListener(mOnClickListener);
//        rp = new RegisterPage();//RegisterPage with surface
//        rp.setRegisterCallback(mEventHandler);

        /**
         * without surface
         */
        SMSSDK.registerEventHandler(mEventHandler);

//        rp.show(this);


    }

    /**
     * event handle
     */
    private EventHandler mEventHandler = new EventHandler() {

        @Override
        public void onRegister() {
            super.onRegister();
            LogUtils.d("--------onRegister");
        }

        @Override
        public void beforeEvent(int i, Object o) {
            super.beforeEvent(i, o);
//            LogUtils.d("-----" + i + " :" + o.toString());
        }

        @Override
        public void afterEvent(int i, int i1, Object o) {
            super.afterEvent(i, i1, o);
            StringBuffer sb = new StringBuffer(256);
            if (i1 == SMSSDK.RESULT_COMPLETE && i == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                ArrayList<HashMap<String, Object>> r = (ArrayList<HashMap<String, Object>>) o;
                for (int j = 0; j < r.size(); j++) {
                    for (HashMap.Entry<String, Object> en : r.get(j).entrySet())
                        LogUtils.d("----k=" + en.getKey() + " : v=" + en.getValue() + "\n");
                    LogUtils.d("------------------------------");
                }

            }
            LogUtils.d(String.valueOf(i) + " il=" + String.valueOf(i1));
//            LogUtils.d("-------------\n" + sb.toString());
        }

        @Override
        public void onUnregister() {
            super.onUnregister();
            LogUtils.d("--------onUnRegister");
        }
    };

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            rp.show(SMSActivity.this);
//            StringBuffer sb = new StringBuffer(256);
            SMSSDK.getSupportedCountries();
            SMSSDK.getVerificationCode(String.valueOf(86), noEt.getText().toString(), mOnSendMessageHandler);
        }
    };


    /**
     * sms handle
     */
    private OnSendMessageHandler mOnSendMessageHandler = new OnSendMessageHandler() {
        @Override
        public boolean onSendMessage(String s, String s1) {
            String ss = s + "    :" + s1;
            return false;
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(mEventHandler);
    }
}
